package SA.teacher.notice;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import SA.teacher.*;
import SA.teacher.answer.AnswerActivity1;
import SA.teacher.daily.DailyActivity1;
import SA.teacher.manage.ManageActivity1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class NoticeView2 extends Activity {

	String id;
	String rcvclassid;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notice2_view);
		Intent intent = getIntent();
		id = intent.getStringExtra("ID");


		//////////////Main Button//////////////////////////////////////
		ImageView mainBtn = (ImageView) findViewById(R.id.mainBtn);
		mainBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mainActivity();
			}
		});
		ImageView manageBtn = (ImageView) findViewById(R.id.manageBtn);
		manageBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				manageActivity();
			}
		});
		ImageView dailyBtn = (ImageView) findViewById(R.id.dailyBtn);
		dailyBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dailyActivity();
			}
		});
		ImageView answerBtn = (ImageView) findViewById(R.id.answerBtn);
		answerBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				answerActivity();
			}
		});
	}

	private class classidparse implements Runnable {
		public void run() {	Log.e("login 占쏙옙占쏙옙 : ", id);
		// TODO Auto-generated method stub
		try {
			URL url = new URL("Http://220.67.128.8/dayclassid.php?id="+ URLEncoder.encode(id, "EUC-KR"));
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			InputStream is = null;
			ByteArrayOutputStream baos = null;
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				is = conn.getInputStream();
				baos = new ByteArrayOutputStream();
				byte[] byteBuffer = new byte[1024];
				byte[] byteData = null;
				int nLength = 0;
				while ((nLength = is.read(byteBuffer)) > 0) {
					baos.write(byteBuffer, 0, nLength);
				}
				byteData = baos.toByteArray();
				String response = new String(byteData, "euc-k-r");
				rcvclassid = response;
				if (response == null || response.equals("")) {
					throw new Exception("Response is NULL!");
				}
			} else {
				Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙 占싫듸옙");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
		}
	}
	//////////////Button �⑥닔��///////////////////////////////////
	void noticeActivity() {
		Intent intent = new Intent(this, NoticeActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void mainActivity() {
		Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void manageActivity() {
		classidparse cidps = new classidparse();
		cidps.run();
		final String[] classes = rcvclassid.split(",");
		if(classes.length-1!=0){
			new AlertDialog.Builder(NoticeView2.this)
			.setTitle("諛섏쓣 �좏깮�댁＜�몄슂.")
			.setItems(classes, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int which) {
					Intent intent = new Intent(NoticeView2.this, ManageActivity1.class);
					intent.putExtra("ID",id);
					intent.putExtra("classname",classes[which]);
					startActivity(intent);
				}
			}).show();
		}
		else{
			new AlertDialog.Builder(NoticeView2.this)
			.setMessage("�꾩옱 �대떦�섎뒗 諛섏씠 �놁뒿�덈떎.")
			.setPositiveButton("�뺤씤", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					// TODO Auto-generated method stub
				}
			}).show();
		}
	}
	void dailyActivity() {
		Intent intent = new Intent(this, DailyActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void answerActivity() {
		Intent intent = new Intent(this, AnswerActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}

}
